package com.app.GoCuro;

import java.util.ArrayList;

import com.app.GoCuro.data.CurrentSession;
import com.app.GoCuro.data.JobsiteInformation;
import com.app.GoCuro.data.LoginResponse;
import com.app.GoCuro.util.CustomHttpClient;


import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class SelectDatabaseActivity extends BaseActivity {

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.select_database);
	    
	    final ListView databaseList = (ListView) findViewById(R.id.database_list);
	   
	    CustomHttpClient client = new CustomHttpClient(this);
	    final ArrayList<JobsiteInformation> userDatabases = client.getUserDatabases();
	    
	    //do not setup the database items if no databases were returned
	    if (userDatabases == null) return;

	    ArrayAdapter<JobsiteInformation> adapter = new ArrayAdapter<JobsiteInformation>(this, android.R.layout.simple_list_item_1, userDatabases);
	    databaseList.setAdapter(adapter);
	    
	    //add the on click listener    
	    databaseList.setOnItemClickListener(new OnItemClickListener()
	    {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3)
			{
				Intent TabLayoutActivityIntent = new Intent(SelectDatabaseActivity.this, TabLayoutActivity.class);

				//place the database into the global state table
				CurrentSession currSession = (CurrentSession) SelectDatabaseActivity.this.getApplicationContext();
				currSession.SetCurrentJobsite(userDatabases.get(pos));
				SelectDatabaseActivity.this.startActivity(TabLayoutActivityIntent);
			}
	    	
	    });
	    
	}

}
